package com.itheima.datastructure.circular;

import java.util.Iterator;

/**
 * @author tantao
 * @version 1.0
 * @description: 环形链表
 * @date 2025/8/13 17:05
 */
public class DoublyLinkedListSentinel01<T> implements Iterable<T> {
    @Override
    public Iterator<T> iterator() {

        return new Iterator<T>() {
            Node p = sentinel.next;

            @Override
            public boolean hasNext() {
                return p != sentinel;
            }

            @Override
            public T next() {
                T value = (T) p.value;
                p = p.next;
                return value;
            }
        };
    }

    static class Node<T> {
        //前驱节点
        public Node prev;
        //数据
        public T value;
        //后继节点
        public Node next;

        public Node(Node prev, T value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }

        @Override
        public String toString() {
            return "Node{" + value + "}";
        }
    }

    private Node<T> sentinel;


    public void addFirst(T value) {
        //哨兵就是头节点
        Node head = sentinel;
        //哨兵下一个就是next
        Node next = sentinel.next;

        Node added = new Node(head, value, next);

        head.next = added;
        next.prev = added;
    }

    public void addLast(T value) {
        //哨兵得前一个就是尾节点
        Node tail = sentinel.prev;
        //哨兵就是下一个节点
        Node next = sentinel;

        Node added = new Node(tail, value, next);

        tail.next = added;
        sentinel.prev = added;
    }


    /**
     * 删除头节点 |a | removed | b|
     */
    public void removeFirst() {
        //哨兵得下一个就是头节点
        Node removed = sentinel.next;
        if (removed == sentinel) {
            throw new IllegalArgumentException("非法");
        }
        Node a = sentinel;
        Node b = removed.next;

        a.next = b;
        b.prev = a;

        removed.prev = null;
        removed.next = null;
    }


    /**
     * 删除尾节点 |a | removed | b|
     */
    public void removeLast() {
        //哨兵得前上一个就是尾节点
        Node removed = sentinel.prev;
        if (removed == sentinel) {
            throw new IllegalArgumentException("非法");
        }
        Node a = removed.prev;
        Node b = sentinel;

        a.next = b;
        b.prev = a;

        removed.prev = null;
        removed.next = null;

    }


    /**
     * 根据值删除节点
     *
     * @param value value
     */
    public void removeByValue(T value) {
        Node removed = findByValue(value);
        if (removed == null) {
            return; // 不用删
        }
        Node a = removed.prev;
        Node b = removed.next;

        a.next = b;
        b.prev = a;
    }


    /**
     * 根据值查找节点(不会找到哨兵)
     *
     * @param value value
     * @return Node
     */
    private Node findByValue(T value) {
        Node p = sentinel.next;
        while (p != sentinel) {
            if (p.value.equals(value)) {
                return p;
            }
            p = p.next;
        }
        return null;
    }


    public DoublyLinkedListSentinel01() {
        //创建一个空链表
        sentinel = new Node(null, "[哨兵]", null);
        sentinel.next = sentinel;
        sentinel.prev = sentinel;
    }

    public static void main(String[] args) {
        DoublyLinkedListSentinel01 doublyLinkedListSentinel01 = new DoublyLinkedListSentinel01();
        System.out.println(doublyLinkedListSentinel01.sentinel);
    }
}
